using MediatR;
using S.M.SmartMedical.Domain.Entities;
using S.M.SmartMedical.Domain.Enums;
using S.M.SmartMedical.ErrorCode;

namespace S.M.SmartMedical.API.Read.Application.Query;

/// <summary>
/// 获取开单申请列表查询
/// </summary>
public class GetOrderApplicationsQuery : IRequest<APIResult<PagedResult<OrderApplicationDto>>>
{
    /// <summary>
    /// 用户ID（可选，用于查询指定用户的申请）
    /// </summary>
    public int? UserId { get; set; }
    
    /// <summary>
    /// 院区ID（可选）
    /// </summary>
    public int? HospitalAreaId { get; set; }
    
    /// <summary>
    /// 申请状态（可选）
    /// </summary>
    public OrderApplicationStatus? Status { get; set; }
    
    /// <summary>
    /// 申请编号（可选，支持模糊搜索）
    /// </summary>
    public string? ApplicationNumber { get; set; }
    
    /// <summary>
    /// 开始日期（可选）
    /// </summary>
    public DateTime? StartDate { get; set; }
    
    /// <summary>
    /// 结束日期（可选）
    /// </summary>
    public DateTime? EndDate { get; set; }
    
    /// <summary>
    /// 页码，从1开始
    /// </summary>
    public int PageIndex { get; set; } = 1;
    
    /// <summary>
    /// 每页大小
    /// </summary>
    public int PageSize { get; set; } = 10;
    
    /// <summary>
    /// 排序字段（可选）：CreateTime, TotalAmount, Status
    /// </summary>
    public string? SortField { get; set; } = "CreateTime";
    
    /// <summary>
    /// 排序方向：true为升序，false为降序
    /// </summary>
    public bool SortAscending { get; set; } = false;
}

/// <summary>
/// 获取单个开单申请详情查询
/// </summary>
public class GetOrderApplicationDetailQuery : IRequest<APIResult<OrderApplicationDetailDto>>
{
    /// <summary>
    /// 申请ID
    /// </summary>
    public int Id { get; set; }
    
    /// <summary>
    /// 用户ID（可选，用于权限验证）
    /// </summary>
    public int? UserId { get; set; }
}

/// <summary>
/// 开单申请数据传输对象
/// </summary>
public class OrderApplicationDto
{
    /// <summary>
    /// 申请ID
    /// </summary>
    public int Id { get; set; }
    
    /// <summary>
    /// 申请编号
    /// </summary>
    public string ApplicationNumber { get; set; } = string.Empty;
    
    /// <summary>
    /// 用户ID
    /// </summary>
    public int UserId { get; set; }
    
    /// <summary>
    /// 用户姓名
    /// </summary>
    public string UserName { get; set; } = string.Empty;
    
    /// <summary>
    /// 院区ID
    /// </summary>
    public int HospitalAreaId { get; set; }
    
    /// <summary>
    /// 院区名称
    /// </summary>
    public string HospitalAreaName { get; set; } = string.Empty;
    
    /// <summary>
    /// 申请状态
    /// </summary>
    public OrderApplicationStatus Status { get; set; }
    
    /// <summary>
    /// 申请状态描述
    /// </summary>
    public string StatusDescription { get; set; } = string.Empty;
    
    /// <summary>
    /// 项目总数量
    /// </summary>
    public int ProjectCount { get; set; }
    
    /// <summary>
    /// 总金额
    /// </summary>
    public decimal TotalAmount { get; set; }
    
    /// <summary>
    /// 备注信息
    /// </summary>
    public string? Remarks { get; set; }
    
    /// <summary>
    /// 预约日期
    /// </summary>
    public DateTime? AppointmentDate { get; set; }
    
    /// <summary>
    /// 预约时间段
    /// </summary>
    public string? AppointmentTimeSlot { get; set; }
    
    /// <summary>
    /// 联系电话
    /// </summary>
    public string? ContactPhone { get; set; }
    
    /// <summary>
    /// 创建时间
    /// </summary>
    public DateTime CreateTime { get; set; }
    
    /// <summary>
    /// 更新时间
    /// </summary>
    public DateTime? UpdateTime { get; set; }
}

/// <summary>
/// 开单申请详情数据传输对象
/// </summary>
public class OrderApplicationDetailDto : OrderApplicationDto
{
    /// <summary>
    /// 申请项目详情列表
    /// </summary>
    public List<OrderApplicationItemDetailDto> Items { get; set; } = new();
}

/// <summary>
/// 开单申请项目详情数据传输对象
/// </summary>
public class OrderApplicationItemDetailDto
{
    /// <summary>
    /// 项目ID
    /// </summary>
    public int Id { get; set; }
    
    /// <summary>
    /// 医疗项目ID
    /// </summary>
    public int MedicalProjectId { get; set; }
    
    /// <summary>
    /// 项目名称
    /// </summary>
    public string ProjectName { get; set; } = string.Empty;
    
    /// <summary>
    /// 项目价格
    /// </summary>
    public decimal ProjectPrice { get; set; }
    
    /// <summary>
    /// 项目描述
    /// </summary>
    public string? ProjectDescription { get; set; }
    
    /// <summary>
    /// 项目类别
    /// </summary>
    public ProjectCategory ProjectCategory { get; set; }
    
    /// <summary>
    /// 项目编码
    /// </summary>
    public string? ProjectCode { get; set; }
    
    /// <summary>
    /// 数量
    /// </summary>
    public int Quantity { get; set; }
    
    /// <summary>
    /// 小计金额
    /// </summary>
    public decimal SubTotal { get; set; }
    
    /// <summary>
    /// 预计时长（分钟）
    /// </summary>
    public int? EstimatedDuration { get; set; }
    
    /// <summary>
    /// 准备说明
    /// </summary>
    public string? PreparationInstructions { get; set; }
}